package cn.smxy.ln.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.smxy.ln.common.dto.LoginDto;
import cn.smxy.ln.pojo.User;
import cn.smxy.ln.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.smxy.ln.common.lang.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
public class AccountController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@Validated @RequestBody LoginDto dto) {
        User user = userService.getOne(new QueryWrapper<User>().eq("user_number", dto.getUserNumber()));
        Result result = new Result();
        if (user == null) {
            result.setMsg("该用户不存在");
            result.setCode("400");
            return result;
        }
        if (!user.getPassword().equals(SecureUtil.md5(dto.getPassword()))) {
            result.setMsg("密码不正确");
            result.setCode("400");
            return result;
        }
        result.setData(user);
        result.setMsg("success");
        result.setCode("200");
        return result;
    }

    //前端清理token，后端不理会
    /*@GetMapping("/logout")
    @ApiOperation("退出")
    public Result logout() {
        //退出登录
        Result result = new Result();
        result.setMsg("注销成功");
        return result;
    }*/
}
